package com.shine.chemicalWholeProcessSystem.ui.riskAssessment;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;

import androidx.annotation.Nullable;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.shine.chemicalWholeProcessSystem.BR;
import com.shine.chemicalWholeProcessSystem.R;
import com.shine.chemicalWholeProcessSystem.app.Constants;
import com.shine.chemicalWholeProcessSystem.base.BaseActivity;
import com.shine.chemicalWholeProcessSystem.bean.DictionariesBean;
import com.shine.chemicalWholeProcessSystem.bean.RiskManagementBean;
import com.shine.chemicalWholeProcessSystem.bean.RiskManagementCreateBean;
import com.shine.chemicalWholeProcessSystem.databinding.ActivityRiskAssessmentCreateBinding;
import com.shine.chemicalWholeProcessSystem.http.ApiException;
import com.shine.chemicalWholeProcessSystem.http.ECHttpResponse;
import com.shine.chemicalWholeProcessSystem.http.ECObserver;
import com.shine.chemicalWholeProcessSystem.http.RetrofitClient;
import com.shine.chemicalWholeProcessSystem.http.RxUtils;
import com.shine.chemicalWholeProcessSystem.http.Service;
import com.shine.chemicalWholeProcessSystem.utils.SPUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class RiskAssessmentCreateActivity extends BaseActivity<ActivityRiskAssessmentCreateBinding, RiskAssessmentCreateViewModel> {
    private boolean isEdit;
    private String id;
    private List<String> dataStrList = new ArrayList<>();
    private RiskManagementCreateBean bean = new RiskManagementCreateBean();

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_risk_assessment_create;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public RiskAssessmentCreateViewModel initViewModel() {
        return new RiskAssessmentCreateViewModel(this);
    }

    @Override
    public int appTheme() {
        return Constants.THEME_FULL;
    }

    @Override
    public void initData() {
        super.initData();
        setStatusBar();
        initTitle(this, "风险研判报告");

        isEdit = getIntent().getBooleanExtra(Constants.BUNDLE_EDIT, false);

        if (isEdit) {
            showView((RiskManagementBean) getIntent().getSerializableExtra(Constants.BUNDLE_EXTRA));
            binding.add.setText("确  认  编  辑");
        } else {
            binding.add.setText("确  认  添  加");
        }

        binding.grade.setOnClickListener(this);
        binding.date.setOnClickListener(this);
        binding.riskControlFacilities.setOnClickListener(this);
        binding.isTrialProduction.setOnClickListener(this);
        binding.startStopStatus.setOnClickListener(this);
        binding.tankFarmWarehouse.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.grade:
                getDictionaries(0);
//                showBottomPop(0, ChooseData.gradeList());
                break;
            case R.id.date:
                Calendar calendar = Calendar.getInstance();
                new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        binding.date.setText(year + "-" + (month + 1) + "-" + dayOfMonth);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH))
                        .show();
                break;
            case R.id.riskControlFacilities:
                getDictionaries(1);
//                showBottomPop(1, ChooseData.yesOrNo());
                break;
            case R.id.isTrialProduction:
                getDictionaries(2);
//                showBottomPop(2, ChooseData.yesOrNo());
                break;
            case R.id.startStopStatus:
                getDictionaries(3);
//                showBottomPop(3, ChooseData.yesOrNo());
                break;
            case R.id.tankFarmWarehouse:
                getDictionaries(4);
//                showBottomPop(4, ChooseData.yesOrNo());
                break;
            default:
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                default:
                    break;
            }
        }
    }

    private void getDictionaries(int type) {
        //风险分类(RiskClass)
        String typeStr = "";
        switch (type) {
            case 0:
                typeStr = "fxypdj";
                break;
            case 1:
                typeStr = "YesOrNo";
                break;
            case 2:
                typeStr = "YesOrNo";
                break;
            case 3:
                typeStr = "YesOrNo";
                break;
            case 4:
                typeStr = "YesOrNo";
                break;
            default:
                break;
        }

        showLoading("");
        RetrofitClient.getInstance().create(Service.class)
                .details(typeStr, SPUtils.getInstance().getString(Constants.SP_TOKEN), SPUtils.getInstance().getString(Constants.SP_MARK))
                .compose(RxUtils.<ECHttpResponse<List<DictionariesBean>>>bindToLifecycle(this))
                .compose(RxUtils.<ECHttpResponse<List<DictionariesBean>>>schedulersTransformer())
                .compose(RxUtils.<ECHttpResponse<List<DictionariesBean>>>exceptionTransformer())
                .subscribe(new ECObserver<List<DictionariesBean>>() {
                    @Override
                    protected void _onNext(List<DictionariesBean> dictionariesBeanList) {
                        dismissLoading();
                        dataStrList.clear();
                        for (DictionariesBean dictionariesBean : dictionariesBeanList) {
                            dataStrList.add(dictionariesBean.getF_ItemName());
                        }
                        showBottomPop(type, dataStrList, dictionariesBeanList);
                    }

                    @Override
                    protected void _onError(ApiException ex) {
                        ToastMessage(ex.message);
                        dismissLoading();
                    }
                });
    }

    private void showView(RiskManagementBean bean) {
        id = bean.getId();
    }

    private String[] data = null;

    private void showBottomPop(int type, List<String> dataList, List<DictionariesBean> dictionariesBeanList) {
        data = dataList.toArray(new String[0]);

        new XPopup.Builder(RiskAssessmentCreateActivity.this)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                .maxHeight(1000)
                .asBottomList("请选择一项", data,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int positionInt, String text) {
                                switch (type) {
                                    case 0:
                                        binding.grade.setText(data[positionInt]);
//                                        bean.setRiskClass(dictionariesBeanList.get(positionInt).getF_ItemValue());
                                        break;
                                    case 1:
                                        binding.riskControlFacilities.setText(data[positionInt]);
                                        break;
                                    case 2:
                                        binding.isTrialProduction.setText(data[positionInt]);
                                        break;
                                    case 3:
                                        binding.startStopStatus.setText(data[positionInt]);
                                        break;
                                    case 4:
                                        binding.tankFarmWarehouse.setText(data[positionInt]);
                                        break;
                                    default:
                                        break;
                                }
                            }
                        })
                .show();
    }
}
